/*Given a string with less than 10^4 characters, encode its substring as "nX" where 'X'
is the only character. For example, "TTTTTEEESLL" would been coded as "5T3ES2L".
Please implement the encoder in C/C++.

Data:2017.04.22

*/


#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string EncodeStr(string source){


    char temp = source[0];
    int times = 1;

    string target;

    for(int i=1; i<source.size(); i++){
        if(temp == source[i]){
            times ++;
        }
        else{
            if(times != 1){
                stringstream ss;
                ss << times;

                target += ss.str();
//                target += temp;
            }
            target += temp;
            times = 1;
            temp = source[i];
        }
    }

    if(times != 1){
        stringstream ss;
        ss << times;

        target += ss.str();
//                target += temp;
    }
    target += temp;


    return target;
}


int main(int argc, char *argv[])
{

    string source = "TTTTTEEESLL";

    cout  << source << endl;

    cout << EncodeStr(source) << endl;

    return 0;
}


/*
stringstream ss;
ss << times;  //times is int
ss.str(); //get string

stringstream ss;
ss.str("108");
double i;
ss >> i;
*/









